﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using Rhino.Mocks;
using MobileBroadbandLoggingMonitorGUI.UI.Interfaces;
using MobileBroadbandLoggingMonitorGUI.Interfaces;
using MobileBroadbandLoggingMonitorUI.UI.Interfaces;
using MobileBroadbandLoggingMonitorGUI.UI;
using Rhino.Mocks.Interfaces;
using System.Windows.Forms;

namespace MobileBroadbandLoggingMonitor.FastTests.GUI
{
    [TestFixture]
    public class ShareInformationPresenterTests
    {
        MockRepository _mockrepository = new MockRepository();
        private IShareInformationView _shareInformationView;
        private IServerCommunication _serverCommunication;
        private IDialogPresenter _dialogPresenter;
        
        [SetUp]
        public void SetUp()
        {
            _shareInformationView = _mockrepository.StrictMock<IShareInformationView>();
            _serverCommunication = _mockrepository.StrictMock<IServerCommunication>();
            _dialogPresenter = _mockrepository.StrictMock<IDialogPresenter>();
        }

        [Test]
        public void UserClicksSendInformationToServer()
        {
            string textToLog = "some information";
            string email = "email";
            string mobileBroadbandCarrierName = "mobileBroadbandCarrierName";
            string mobileBroadbandSoftwareName = "mobileBroadbandSoftwareName";

            IEventRaiser shareRaiser = null;
            IEventRaiser cancelRaiser = null;
                
            using (_mockrepository.Record())
            {
                Expect.Call(_shareInformationView.ShowDialog()).Return(DialogResult.OK);
                Expect.Call(() => _shareInformationView.ShareClicked += null).IgnoreArguments();
                shareRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                Expect.Call(() => _shareInformationView.CancelClicked += null).IgnoreArguments();
                cancelRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                Expect.Call(_shareInformationView.InformationToBeShared = textToLog);
                Expect.Call(_shareInformationView.Email).Return(email);
                Expect.Call(_shareInformationView.MobileSoftwareName).Return(mobileBroadbandSoftwareName);
                Expect.Call(_shareInformationView.MobileBroadbandCarrierName).Return(mobileBroadbandCarrierName);
                Expect.Call(() => _serverCommunication.SendSharedInformationToServer(mobileBroadbandCarrierName, mobileBroadbandSoftwareName, textToLog, email));
                Expect.Call(() =>_shareInformationView.Close());
            }

            using (_mockrepository.Playback())
            {
                ShareInformationPresenter shareInformationPresenter = new ShareInformationPresenter(_shareInformationView, _serverCommunication, _dialogPresenter);
                shareInformationPresenter.Show(textToLog);
                shareRaiser.Raise(new object(), new EventArgs());
            }
        }

        [Test]
        public void UserClicksSendAndDialogisShownDueToException()
        {
            string textToLog = "some information";
            string email = "email";
            string mobileBroadbandCarrierName = "mobileBroadbandCarrierName";
            string mobileBroadbandSoftwareName = "mobileBroadbandSoftwareName";

            IEventRaiser shareRaiser = null;
            IEventRaiser cancelRaiser = null;

            using (_mockrepository.Record())
            {
                Expect.Call(_shareInformationView.ShowDialog()).Return(DialogResult.OK);
                Expect.Call(() => _shareInformationView.ShareClicked += null).IgnoreArguments();
                shareRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                Expect.Call(() => _shareInformationView.CancelClicked += null).IgnoreArguments();
                cancelRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                Expect.Call(_shareInformationView.InformationToBeShared = textToLog);
                Expect.Call(_shareInformationView.Email).Return(email);
                Expect.Call(_shareInformationView.MobileSoftwareName).Return(mobileBroadbandSoftwareName);
                Expect.Call(_shareInformationView.MobileBroadbandCarrierName).Return(mobileBroadbandCarrierName);
                Expect.Call(() => _serverCommunication.SendSharedInformationToServer(mobileBroadbandCarrierName, mobileBroadbandSoftwareName, textToLog, email)).Throw(new Exception());
                Expect.Call(_dialogPresenter.ShowDialog(null, null, MessageBoxButtons.OK, MessageBoxIcon.Error)).IgnoreArguments().Return(DialogResult.OK);
            }

            using (_mockrepository.Playback())
            {
                ShareInformationPresenter shareInformationPresenter = new ShareInformationPresenter(_shareInformationView, _serverCommunication, _dialogPresenter);
                shareInformationPresenter.Show(textToLog);
                shareRaiser.Raise(new object(), new EventArgs());
            }
        }


        [Test]
        public void UserClicksCancel()
        {
            string textToLog = "some information";
            string email = "email";
            string mobileBroadbandCarrierName = "mobileBroadbandCarrierName";
            string mobileBroadbandSoftwareName = "mobileBroadbandSoftwareName";

            IEventRaiser shareRaiser = null;
            IEventRaiser cancelRaiser = null;

            using (_mockrepository.Record())
            {
                Expect.Call(_shareInformationView.ShowDialog()).Return(DialogResult.OK);
                Expect.Call(() => _shareInformationView.ShareClicked += null).IgnoreArguments();
                shareRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                Expect.Call(() => _shareInformationView.CancelClicked += null).IgnoreArguments();
                cancelRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                Expect.Call(_shareInformationView.InformationToBeShared = textToLog);
                Expect.Call(() => _shareInformationView.Close());
            }

            using (_mockrepository.Playback())
            {
                ShareInformationPresenter shareInformationPresenter = new ShareInformationPresenter(_shareInformationView, _serverCommunication, _dialogPresenter);
                shareInformationPresenter.Show(textToLog);
                cancelRaiser.Raise(new object(), new EventArgs());
            }
        }

    }
}
